|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
The MsgBox Function The MsgBox function is one of Visual Basics most useful tools. You use it to display brief messages to the user and get a response. In its simplest form, MsgBox displays your message in a small dialog box containing an OK button. The title of the dialog box is the same as the project name. For example, the statement MsgBox(This is the message) displays the box shown in Figure 11.5. Program execution pauses while the box is displayed and continues once the user has selected OK. The MsgBox function is a great deal more flexible than this, however. You can specify your own title as well as the display of different combinations of buttons (Yes and No buttons, for example). When more than one button is displayed, the return value of the MsgBox function indicates which button the user selected. You can also have a graphical icon, such as a question mark, displayed in the message box. Here is the full syntax of the MsgBox function: return = MsgBox(message, flags, title) Lets take a brief tour of the arguments:
For the flags argument, you can use the predefined constants listed in Table 11.2. The last two constants determine which button is the defaultthat is, which one will be selected if the user simply presses Enter. The normal default is the first button. To combine constantsto specify buttons and an icon, for exampleuse the Or operator. The following call would display the Abort, Retry, and Ignore buttons, an exclamation icon, and make the Retry button the default:
x = MsgBox(Message Here, vbAbortRetryIgnore Or vbExclamation Or _
vbDefaultButton2, Title Here)
The possible return values are also defined by constants, which are listed in Table 11.3.
Creating The SaveChanges Function Go ahead now and create the SaveChanges function, assigning it a return type of Boolean. The function code is shown in Listing 11.14. Note that the code does not explicitly test for a return from the message box of vbNo. If the user selects No (the only possibility besides Yes and Cancel), execution falls through the If block, and the function terminates with a return value of True. Listing 11.14 The SaveChanges function.
Private Function SaveChanges() As Boolean
Determines if the text being edited has changed since the
last File|Save command. If so, the function offers the user the option
of saving the file.
Function returns True if there have been no changes or if
the user saves the changes; returns False if the user
selects Cancel.
Dim Title As String, Msg As String
Dim Reply As Integer, Flags As Integer
If TextChanged = True Then
Title = Text has changed
Msg = Save changes to text?
Flags = vbYESNOCANCEL + vbQUESTION
Reply = MsgBox(Msg, Flags, Title)
If Reply = vbYES Then
Call SaveFile
ElseIf Reply = vbCANCEL Then
SaveChanges = False
Exit Function
End If
End If
SaveChanges = True
End Function
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |